package com.sctvcloud.qingchuan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.beans.WarningItemBean;

import java.util.ArrayList;
import java.util.List;

public class WarningAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<WarningItemBean> warningList = new ArrayList<>();
    private Context ctx;
    private OnItemBtnClick listener;

    public WarningAdapter(Context ctx, List<WarningItemBean> lList, OnItemBtnClick listener) {
        this.ctx = ctx;
        this.warningList = lList;
        this.listener = listener;
    }

    static class WarningViewHolder extends RecyclerView.ViewHolder {
        CustomFontTextView title;
        CustomFontTextView rightIcon;
        RelativeLayout itemLayout;

        public WarningViewHolder(View view) {
            super(view);
            title = (CustomFontTextView) view.findViewById(R.id.warning_title);
            rightIcon = (CustomFontTextView) view.findViewById(R.id.right_icon);
            itemLayout = (RelativeLayout) view.findViewById(R.id.item_layout);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(ctx).inflate(R.layout.item_warning_list, parent, false);
        holder = new WarningViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (null != warningList && warningList.size() > position) {
            final WarningItemBean warning = warningList.get(position);
            WarningViewHolder h = (WarningViewHolder) holder;
            if (warning.isMore()) {
                h.rightIcon.setVisibility(View.VISIBLE);
            } else {
                h.rightIcon.setVisibility(View.GONE);
            }
            h.title.setText(warning.getTitle());
            h.itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onItemBtnClick(warning.getTitle(), position,warning.isMore());
                    }
                }
            });
            if (warning.isSelected()) {
                h.title.setTextColor(ctx.getResources().getColor(R.color.colorHomeText));
            } else {
                h.title.setTextColor(ctx.getResources().getColor(R.color.colorBlack33));
            }
        }
    }

    public void setDataList(List<WarningItemBean> list) {
        this.warningList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (null != warningList) {
            return warningList.size();
        } else {
            return 0;
        }
    }

    public interface OnItemBtnClick {
        void onItemBtnClick(String title, int postion, boolean isMore);
    }
}


